(function() {
  var ADIMG_URL = '/api/adimages?status=1'
  var HOTCOURSES_URL = '/api/products?isHot=1&status=2'
  var TEACHERS_URL = '/api/teachers?status=1'
  var INTRO_URL = '/api/agency'

  function loadBanner(data) {
    var htmlStr = ''
    $.each(data, function (i, item) {
      htmlStr += '<li class="swiper-slide" style="background-image: url('+ item.image +')">\
        <a target="_blank" '+ (item.url?('href="'+ item.url +'"'):'') +'>\
        </a>\
      </li>'
    })

    $('#J_swiper').html(htmlStr)

    var mySwiper = new Swiper('.swiper-container',{
      pagination: '.swiper-pagination',
      slidesPerView: 1,
      paginationClickable: true,
      autoplayDisableOnInteraction: false,
      // spaceBetween: 30,
      keyboardControl: true,
      nextButton: '.swiper-button-next',
      prevButton: '.swiper-button-prev',
      paginationClickable: true,
      loop: true,
      // autoplay: false,
      autoplay: 5000
    });
  }

  function loadHotCourses(data) {

    var htmlStr = ''
    $.each(data, function (i, item) {

      if(i > 7) {
        return false;
      }

      htmlStr += '<a href="course.html?id='+ item.id +'" class="course">\
        <div class="uk-thumbnail">\
          <img class="lazy" data-original="'+ item.cover +'" src="/home/img/lazyload_course.png" alt="">\
          <h3 class="uk-text-truncate">'+ item.title +'</h3>\
          <div>\
            <span>'+i18next.t('home.teacher')+'：<span>'+ (item.masterinfo ? item.masterinfo.lastname : '') +'</span></span>\
            <span class="price">'+ (item.price === '0.00'? i18next.t('home.free'):('￥'+item.price)) +'</span>\
          </div>\
          <div class="uk-clearfix" style="margin-top: 8px;">\
            <div class="uk-float-left detail">\
              '+i18next.t('home.browse')+'：<span class="visit">'+ (item.visited||0) +'</span>'+i18next.t('home.buy')+'：<span class="buy">'+ (item.sold||0) +'</span>\
            </div>\
            <button class="uk-button uk-button-primary uk-button-small uk-float-right">'+i18next.t('home.buy')+'</button>\
          </div>\
        </div>\
      </a>'
    })

    $('#J_hotcourse').html(htmlStr)
  }

  function loadTeacher(data) {
    var htmlStr = ''
    $.each(data, function (i, item) {

      if(i > 3) {
        return false;
      }

      var defaultAvatar = ""

      htmlStr += '<a href="teacher.html?id='+ item.teacheruid +'" class="teacher">\
        <div class="uk-panel uk-panel-box uk-text-center">\
          <img class="lazy uk-border-circle" width="120" height="120" data-original="'+ item.photo +'" src="'+ (defaultAvatar) +'" alt="">\
          <h3 class="uk-margin-bottom-remove">'+ item.lastname +'</h3>\
          <p class="uk-text-large uk-margin-top-remove uk-text-muted">'+ $.map(item.coursedesc, function (i, item) {
            return item.coursetypename
          }).join(', ') +'</p>\
          <p class="uk-text-left cc-text-truncate-multi">'+ item.introduce +'</p>\
        </div>\
      </a>'
    })

    $('#J_teachers').html(htmlStr)
  }

  function loadAgency(data) {
    if(data && data.image){
      $('#J_custom').append('<img class="lazy" data-original="'+ data.image +'" alt="" />')
    }else{
      $('#J_custom').hide();
    }
  }

  $.when(
    $.ajax(ADIMG_URL),
    $.ajax(HOTCOURSES_URL),
    $.ajax(TEACHERS_URL),
    $.ajax(INTRO_URL)
  ).done(function () {
    var cbFnArr = [
      loadBanner,
      loadHotCourses,
      loadTeacher,
      loadAgency
    ]

    var resArr = [].slice.call(arguments,0,4)
    $.each(resArr, function (i, curRes) {
      if(curRes[1] == 'success') {
        cbFnArr[i](curRes[0].data)
      }
    })

    imgLazyload()

  })
}());
